﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using YetAnotherChat.TcpServer.WebSocketProtocols;

namespace YetAnotherChat.TcpServer
{
    /// <summary>
    /// Use header class to select protocol to return.
    /// </summary>
    public class WebSocketProtocolFactory
    {
        public IWebSocketProtocol ProcessConnectRequest(Connection connection)
        {
            string data = Encoding.UTF8.GetString(connection.Buffer, 0, connection.ReceivedByteCount);
            Debug.WriteLine(string.Format("Process http/websocket headers"));
            connection.Header = new Header(data);
            switch (connection.Header.Protocol)
            {
                case Protocol.WebSocketHybi00:
                    Debug.WriteLine(string.Format("Create WebSocketHybi00 from factory"));
                    return new WebSocketProtocols.hybi00.WebSocketHyBi00Protocol(new WebSocketProtocols.hybi00.Authentication()) { DataFrame = new WebSocketProtocols.hybi00.DataFrame() };

                case Protocol.WebSocketRFC6455:
                    Debug.WriteLine(string.Format("Create WebSocketRFC6455 from factory"));
                    return new WebSocketProtocols.rfc6455.WebSocketRfc6455Protocol(new WebSocketProtocols.rfc6455.Authentication()) { DataFrame = new WebSocketProtocols.rfc6455.DataFrame() };

            }
            return null;
        }
    }
}
